<template>
  <div class="box" v-loading="loading">
    <div class="box-title" v-if="title">
      <div class="title font-color" v-html="title"></div>
      <div class="btn" v-if="hasBtn">
        <div v-for="(item, index) in btnType" :key="index" @click="btnClick(index)"
             :class="{'active': currentType == item}">
          {{ item }}
        </div>
      </div>
    </div>
    <div class="box-echart">
      <slot name="chart" style="position: absolute;z-index: 999999"></slot>
      <slot></slot>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      currentType: '近七天',
      loading: false
    }
  },
  props: {
    boxId: {
      type: String,
      default: ''
    },
    btnType: {
      type: Array,
      default () {
        return ['近七天', '月']
      }
    },
    title: {
      type: String,
      require: ''
    },
    hasBtn: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    btnClick (index) {
      this.currentType = this.btnType[index]
      this.$emit('refreshEchart', {
        currentType: this.currentType,
        boxId: this.boxId
      })
      this.showLoading()
    },
    showLoading () {
      this.loading = true
    },
    closeLoading () {
      this.loading = false
    }
  }
}
</script>
<style scoped lang="scss">
$commonColor: rgba(99, 156, 255, 1);
.box {
  width: 100%;
  flex: 1;
  margin: 5px 0;
  background-image: url('~@/assets/img/content.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  padding: 20px 20px;
  overflow: hidden;

  .box-title {
    height: 20px;
    // width: 80%;
    background: url('~@/assets/img/box-title.png') no-repeat;
    display: flex;
    align-items: center;

    .title {
      width: 100%;
      height: 28px;
      font-size: 16px;
      font-family: PingFang SC-Medium, PingFang SC;
      font-weight: 500;
      line-height: 28px;
      padding-left: 10px;
    }
  }

  .box-echart {
    width: 100%;
    height: 100%;
    position: relative;

    div {
      width: 100%;
      height: 100%;
    }
  }
}

.btn {
  box-sizing: border-box;
  display: flex;
  color: $commonColor;
  border: 1px solid $commonColor;
  text-align: center;
  font-size: 12px;
  height: 30px;
  line-height: 30px;
  cursor: pointer;

  div {
    box-sizing: border-box;

    white-space: nowrap;
    padding: 0 10px;

    &:not(:last-child) {
      padding: 0;
      font-size: 10px;
      border-right: 1px solid $commonColor;
    }

    &:hover {
      color: white;
      background-color: $commonColor;
    }
  }

  .active {
    color: white;
    background-color: $commonColor;
  }
}
</style>
